package com.yhy.common.security.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yhy.common.core.utils.WebUtils;
import com.yhy.common.security.constant.SecurityConstants;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * JWT工具类
 */
@Slf4j
@UtilityClass
public class JwtUtil {

    @SneakyThrows
    public JSONObject getJwtPayload() {
        JSONObject jsonObject = null;
        String payload = WebUtils.getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
        if (StrUtil.isNotBlank(payload)) {
            jsonObject = JSONUtil.parseObj(URLDecoder.decode(payload, StandardCharsets.UTF_8.name()));
        }
        return jsonObject;
    }
}
